package com.nf.health.app.intervention_details.fragment;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nf.health.app.R;
import com.nf.health.app.core.MyGsonBuilder;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.http.Act;
import com.nf.health.app.intervention_details.activity.InterventionDetailsActivity;
import com.nf.health.app.intervention_details.adapter.FollowupAdapter;
import com.nf.health.app.intervention_details.bean.QuestionListBean;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ViewUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 随访问卷
 *
 * @author changpeng
 * @time 2016/11/17
 */
public class FollowupFragment extends IBaseFragment {
    private RecyclerView mRecyclerView;
    private FollowupAdapter mFollowupAdapter;
    private List<QuestionListBean> mQuestionListBean;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.common_layout_recycler);
        mRecyclerView = ViewUtil.getView(rootView, R.id.recycler_view_common);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setBackgroundColor(getResources().getColor(R.color.white));
    }

    private void initData() {
        provider.getQuestionList();
    }

    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if (actionName.equals(Act.Flag.GET_QUESTION_LIST)) {
            Gson gson = new MyGsonBuilder().createGson();
            Type type = new TypeToken<ArrayList<QuestionListBean>>() {
            }.getType();
            mQuestionListBean = gson.fromJson(object + "", type);
            if (mQuestionListBean == null) {
                return;
            }

            Collections.sort(mQuestionListBean, new Comparator<QuestionListBean>() {
                @Override
                public int compare(QuestionListBean lhs, QuestionListBean rhs) {
                    Date quesTimeDate1 = DateUtil.string2Date(lhs.getSurveytime(),"yyyy-MM-dd");
                    Date quesTimeDate2 = DateUtil.string2Date(rhs.getSurveytime(),"yyyy-MM-dd");

                    //对日期字段进行升序，如果欲降序可采用before方法
                    if(quesTimeDate1.after(quesTimeDate2)) return 1;
                    return -1;
                }
            });
            String hname = ((InterventionDetailsActivity) getActivity()).getHnick();
            mFollowupAdapter = new FollowupAdapter(getActivity(), mQuestionListBean, hname);
            mRecyclerView.setAdapter(mFollowupAdapter);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }
}
